import { Component, OnInit,Injectable ,ViewChild,ElementRef,Input} from '@angular/core';
import { MainService }   from './main.serve';
import 'rxjs/add/observable/of';
import { PubuService } from 'app/main/pubu/pubu.serve'
//传递参数
import { Observable } from 'rxjs/Observable';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { ModalModule} from 'ng2-bootstrap';
import { Subject}  from 'rxjs/Subject'



 declare let $:any;
@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  providers:[ MainService,PubuService ]
})
export class MainComponent implements OnInit {

  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private mainService:MainService,
    private pubuService:PubuService,
  ){ }
  @ViewChild('lgModal') public _lgModal;
  @Input() onHide;
  public errorMessage:string;
  public searchtext:Subject<string> = new Subject<string>();
      public heroes='';
      private id='';
      public name='';
      private statu:any;
      private showfrom=true;
      private inper=true;
      private sta=[];
      private lashow=true;
      public keyt:string;
      private getval='';
      private listda='';
      private sat='';
  //获取服务器参数
      getHeroes(email:string, pwd:string,invitation:string) {
           this.mainService.login(email,pwd,invitation)
             .subscribe(
               res => {this.heroes = res['username'];
                 this.id = res['userid'];
                 this.statu  = res['status'];
                 let sta =this.statu;
                 if(sta=='00'){
                   this.showfrom=true;
                   this._lgModal.hide();
                   this.inper=false;
                 }else {
                   this.showfrom=false;
                   this.inper=true
                 }
               },
               error =>this.errorMessage = <any>error);
      }
      gopercent(){
        this.router.navigate(['/linkPersonal']);
      }
      showbac(){
        this.lashow=false;
      }
  hidbac(){
    this.lashow=true;
  }

      ngOnInit() {

       this.mainService.loginchek()
       .subscribe(
        res=>{this.sat=res['status'];
          let sta =this.sat;
          if(sta=='00'){
            this.inper=false;
          }else {
            this.inper=true;
          }
          console.log(sta)
        }

      );
        this.searchtext
      .debounceTime(500)
      .subscribe(
        keyt=>{
          this.getval=keyt;
          console.log(keyt);
          let govaul=this.getval;
          this.router.navigate(['/link-center',govaul]);
          this.pubuService.grtinfro(govaul)
            .subscribe(
              res=>{this.sta=res['data'];
              }
            );
        });

      }
      golistsearch(govaul:string){
        $(function () {
          $('.arrow ').click(function () {
            $(this).addClass('active');
            $(this).siblings().removeClass('active');
          })
        });
        let secondsta=this.statu;
        if(secondsta=='00'){
          this.router.navigate(['/link-center',govaul]);
        }else {
          alert("您还没有登陆，搜索功能不可用");
        }
      }
      getlistitem(){
        this.mainService.category()
          .subscribe(
            res=>{this.listda=res['data'];
            }
          )
      }
      // public gopubu($event):void{
      //    let govaul=this.getval;
      //   this.router.navigate(['/link-center',govaul]);
      // }
      public searchchange($event):void {
         this.searchtext.next(this.keyt)
      }




  toptsh =false;
  tShow(){
    if (this.toptsh==true){
      this.toptsh=false
    }else {
      this.toptsh=true
    }
  }

}
